from collections import defaultdict


class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        '''
        利用字典，每次比较字典的长度
        '''
        dic = {}
        for ch in s:
            dic[ch] = dic.get(ch, 0) + 1
        for ch in t:
            if ch not in dic:
                return False
            dic[ch] -= 1
            if dic[ch] == 0:
                del dic[ch]
        return len(dic) == 0

    def isAnagram2(self, s: str, t: str) -> bool:
        if len(s) != len(t): 
            return False
        dic = defaultdict(int)
        for ch in s:
            dic[ch] += 1

        for ch in t:
            if dic[ch] == 0:
                return False
            dic[ch] -=1
        return True
